package energyDown.gui;

import energyDown.modelo.UsoAparelho;
import energyDown.modelo.unidade.Tempo;
import java.util.List;


/**
 *
 * @author Ana Meireles
 */
public class DialogEdicaoAparelhosComodo extends javax.swing.JDialog {
    
    private AparelhoTableModel tableModel;

    public DialogEdicaoAparelhosComodo() {
    }
    
    /**
     * Creates new form NewJDialog
     */
    public DialogEdicaoAparelhosComodo(List<UsoAparelho> aparelhos) {
        super();
        setTitle("Alterar Uso do Aparelho");
        tableModel = new AparelhoTableModel(aparelhos);
        initComponents();
        tabelaAparelhosComodo.setModel(tableModel);
        /*labelDescricao.setText(aparelho.getDescricao());
        txtPotencia.setText(String.valueOf(aparelho.getPotencia()));
        spnTempoUso.setValue(aparelho.getTempoUso());
        
        cbxUnidadeTempo.removeAllItems();
        for (Tempo unidade : Tempo.values()) {
            cbxUnidadeTempo.addItem(unidade);
        }
        cbxUnidadeTempo.setSelectedItem(aparelho.getUnidadeDeTempo());*/
    }
    
    private UsoAparelho getAparelhoSelecionado(){
        int linhaTabela = tabelaAparelhosComodo.getSelectedRow();
        int indiceLista = tabelaAparelhosComodo.convertRowIndexToModel(linhaTabela);
        return tableModel.getUsoAparelho(indiceLista);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaAparelhosComodo = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        labelDescricao = new javax.swing.JLabel();
        labelPotencia = new javax.swing.JLabel();
        labelTempoUso = new javax.swing.JLabel();
        labelUnidadeTempo = new javax.swing.JLabel();
        cbxUnidadeTempo = new javax.swing.JComboBox();
        spnTempoUso = new javax.swing.JSpinner();
        txtPotencia = new javax.swing.JTextField();
        botaoOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sala");

        tabelaAparelhosComodo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"TV de tubo 29\""},
                {"Home theater"},
                {"Lâmpada incandescente 80W"}
            },
            new String [] {
                "APARELHOS"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabelaAparelhosComodo);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        labelDescricao.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelDescricao.setText("TV de tubo 29\"");

        labelPotencia.setText("Potência (W)");

        labelTempoUso.setText("Tempo de uso");

        labelUnidadeTempo.setText("Unidade de tempo");

        cbxUnidadeTempo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "HORAS/DIA" }));

        txtPotencia.setEditable(false);
        txtPotencia.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtPotencia.setText("80");

        botaoOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javaapplication1/ok01.png"))); // NOI18N
        botaoOK.setText("CONFIRMAR");
        botaoOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelDescricao)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUnidadeTempo)
                            .addComponent(labelTempoUso)
                            .addComponent(labelPotencia))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbxUnidadeTempo, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(spnTempoUso, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                                .addComponent(txtPotencia, javax.swing.GroupLayout.Alignment.LEADING))))
                    .addComponent(botaoOK, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelDescricao)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPotencia)
                    .addComponent(txtPotencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTempoUso)
                    .addComponent(spnTempoUso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelUnidadeTempo)
                    .addComponent(cbxUnidadeTempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botaoOK)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoOKActionPerformed
        UsoAparelho aparelho = getAparelhoSelecionado();
        aparelho.setUnidadeDeTempo((Tempo) cbxUnidadeTempo.getSelectedItem());
        aparelho.setTempoUso((Integer) spnTempoUso.getValue());
    }//GEN-LAST:event_botaoOKActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogEdicaoAparelhosComodo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogEdicaoAparelhosComodo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogEdicaoAparelhosComodo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogEdicaoAparelhosComodo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DialogEdicaoAparelhosComodo dialog = new DialogEdicaoAparelhosComodo();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoOK;
    private javax.swing.JComboBox cbxUnidadeTempo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelDescricao;
    private javax.swing.JLabel labelPotencia;
    private javax.swing.JLabel labelTempoUso;
    private javax.swing.JLabel labelUnidadeTempo;
    private javax.swing.JSpinner spnTempoUso;
    private javax.swing.JTable tabelaAparelhosComodo;
    private javax.swing.JTextField txtPotencia;
    // End of variables declaration//GEN-END:variables

}